import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/widgets/header.dart";
import "package:flutter/material.dart";

class PageViewSwitcher extends StatefulWidget {
  final List<String> items;
  final double itemWidth;
  final List<Widget>? actions;
  final List<Widget> children;
  final int? current;
  final PageController? controller;

  const PageViewSwitcher({
    super.key,
    required this.items,
    this.itemWidth = 100,
    this.actions,
    required this.children, this.current, this.controller,
  });

  @override
  State<PageViewSwitcher> createState() => _PageViewSwitcherState();
}

class _PageViewSwitcherState extends State<PageViewSwitcher> {
  late final PageController _controller;
  late final double _itemWidth = widget.itemWidth;
  int _current = 0;

  Widget _renderTitle() {
    int length = widget.items.length;
    return SizedBox(
      width: length * _itemWidth,
      height: 40,
      child: Stack(
        alignment: Alignment.center,
        children: [
          SizedBox.expand(
            child: DecoratedBox(
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10),
                color: ThemeVars.colorBackground,
              ),
            ),
          ),
          AnimatedPositioned(
            top: 3,
            bottom: 3,
            left: _current * _itemWidth + 3,
            width: _itemWidth - 6,
            duration: Durations.short3,
            child: DecoratedBox(
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(7),
                boxShadow: const [ThemeVars.boxShadow],
                color: Colors.white,
              ),
            ),
          ),
          Row(
            children: List.generate(length, (index) {
              String ele = widget.items[index];
              return Expanded(
                child: GestureDetector(
                  behavior: HitTestBehavior.translucent,
                  onTap: () {
                    setState(() => _current = index);
                    _controller.jumpToPage(index);
                  },
                  child: Container(
                    alignment: Alignment.center,
                    height: 40,
                    child: Text(
                      ele,
                      style: const TextStyle(
                        color: ThemeVars.colorText,
                        fontSize: 14,
                      ),
                      textAlign: TextAlign.center,
                    ),
                  ),
                ),
              );
            }),
          ),
        ],
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    if (widget.controller != null) {
      setState(() {
        _controller = widget.controller!;
        _current = _controller.initialPage;
      });
    } else {
      _controller = PageController();
    }

    // if (widget.current != null) {
    //   setState(() {
    //     print("水水水水:>>>>${widget.current}");
    //     _current = widget.current!;
    //   });
    //   _controller.jumpToPage(_current);
    // }

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: Header(
        actions: widget.actions,
        title: _renderTitle(),
      ),
      body: PageView(
        controller: _controller,
        onPageChanged: (page) => setState(() => _current = page),
        children: widget.children,
      ),
    );
  }
}
